﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;


namespace Homebound
{
    class PauseState : State
    {
        GamePlayState gamePlayState;
        SpriteFont keyFont;
        TimeSpan flashSpan;
        Color[] colours;
        public bool nextEnabled;
        int index;
        const int MAX_COLOURS = 2;
       
        public PauseState(GraphicsDeviceManager newGraphics, GameServiceContainer newServices, ContentManager newContent)
            : base(newGraphics, newServices, newContent)
        {
            flashSpan = TimeSpan.FromSeconds(2);
            colours = new Color[MAX_COLOURS];
            index = 0;
            colours[0] = Color.Red;
            colours[1] = Color.White;
            nextEnabled = false;
        }

        public override void loadContent()
        {
            keyFont = content.Load<SpriteFont>("Fonts/Hud");
 	        base.loadContent();
        }

        // Called whenever a new state is selected.
        public override void onEnter(State state)
        {
            loadContent();

            gamePlayState = (GamePlayState)state;
            base.onEnter(state);
        }

        // Called when the state is changing.
        public override void onExit(State state)
        {
            base.onExit(state);
        }

        // Draw the visual representation of the state.
        public override void draw(SpriteBatch spriteBatch, GameTime gameTime, Vector2 screenSize)
        {
            Vector2 textPos = new Vector2(screenSize.X / 2, screenSize.Y / 2);

            // Draw the gameplay paused.
            if (gamePlayState != null) // The gameplay state must have been added.
            {
                gamePlayState.draw(spriteBatch, gameTime, screenSize);
            }

            // Draw the game pause text.
            spriteBatch.Begin();
            spriteBatch.DrawString(keyFont, "GAME PAUSED", textPos, colours[index]);
            spriteBatch.End();
            base.draw(spriteBatch, gameTime, screenSize);
        }

        // Handles all input for the current state, (Keyboard, GamePad).
        public override void input(GameTime gameTime, List<Keys> keys, GamePadButtons buttons, GamePadDPad dpad, GamePadThumbSticks thumbSticks)
        {
            if ((Keyboard.GetState().IsKeyDown(Keys.F10) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Start)) && nextEnabled)
            {
                gamePlayState.pause = false;
                nextEnabled = false;
            }

            if (Keyboard.GetState().IsKeyUp(Keys.F10) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.Start))
            {
                gamePlayState.pause = true;
                nextEnabled = true;
            }
        }

        // Handles all update operations for the current state, (Keyboard, GamePad).
        public override void update(GameTime gameTime)
        {
            // Change colour of text every two seconds.
            if (gameTime.ElapsedGameTime.Seconds >= flashSpan.Seconds)
            {
                index = (index + 1) % MAX_COLOURS;
            }
            base.update(gameTime);
        }

        // Conditions necessary to enter go return to a next state.
        public override bool goingForward(State prev)
        {
            GamePlayState gamePlayState = (GamePlayState)prev;

            return (gamePlayState.pause);
        }
    }
}